package com.jpz.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.zip.ZipInputStream;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.jpz.bean.Applytype;
import com.jpz.bean.User;
import com.jpz.service.ApplytypeService;
import com.jpz.service.Userservice;

@Controller
@RequestMapping("/process")
public class Processcontroller {
	
	@Autowired
	ProcessEngine pe;

	@Autowired
	Userservice us;
	
	@Autowired
	ApplytypeService ats;
	/*
	 *审批流程的查询 
	 */
	@RequestMapping("/show")
	public String show(Model model) {
		ProcessDefinitionQuery query = pe.getRepositoryService().createProcessDefinitionQuery();
		List<ProcessDefinition> list = query.list();
		//将查询到的集合带到页面显示
		model.addAttribute("plist", list);
		return "/flow_ProcessDefinitionList.jsp";
	}
	
	
	@RequestMapping("/up")
	public void upfile(@RequestParam("mFile") MultipartFile file,Model model,HttpServletRequest request,HttpServletResponse response) throws Exception {
		//
		File path = createDir(request.getSession().getServletContext());
		String fileName=createName(file.getOriginalFilename());
		File f=new File(path, fileName);
		if (!file.isEmpty()) {
			try {
				file.transferTo(f);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		//对路径的操作
		String absolutePath = f.getAbsolutePath();
		int indexOf = absolutePath.indexOf("Dida");
		String substring = absolutePath.substring(indexOf+5);
		String replaceAll = substring.replaceAll("\\\\", "/");
		//存值到页面改变后  用于显示
		System.out.println("上传成功  获取的路径"+replaceAll);
		System.out.println("绝对路径"+absolutePath);
		//上传成功之后 开始部署到服务器上面
		DeploymentBuilder deploymentBuilder = pe.getRepositoryService().createDeployment();
		//获取流 读取文件
		FileInputStream fis = new FileInputStream(absolutePath);
		ZipInputStream zipinput = new ZipInputStream(fis);
		deploymentBuilder.addZipInputStream(zipinput);
		String name="请假流程";
		deploymentBuilder.name(name);
		Deployment deployment = deploymentBuilder.deploy();
		System.out.println("部署成功 获取的ID"+deployment.getId()+"获取的名字"+deployment.getName());
		//部署成功之后查询key 便于后面启动
		ProcessDefinitionQuery query = pe.getRepositoryService().createProcessDefinitionQuery();
		query.orderByProcessDefinitionVersion().desc();
		ProcessDefinition pd = query.list().get(0);
		String key = pd.getKey();
		//将获取到的数据存入数据库
		Applytype applytype = new Applytype(key, name);
		boolean insert = ats.insert(applytype);
		if(insert) {
			response.getWriter().write(
					"<script>alert('部署成功');location.href='"
							+ request.getContextPath() + "/main.jsp;'</script>");
		}
	}
		

	
	
	// 创建目录---以日期，一天一个文件夹
			private File createDir(ServletContext context) {
				String realPath = context.getRealPath("/process");
				String date = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
				File file = new File(realPath, date);
				if (!file.exists()) {
					file.mkdir();
				}
				return file;
			}

			// 创建文件名--区分同名文件,在文件名前加上当前的时间
			private String createName(String name) {
				return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar.getInstance().getTime()) + "_" + name;
			}

}
